package com.njcb.game.web.controller;

import com.google.common.collect.Maps;
import com.njcb.ams.pojo.dto.standard.EntityResponse;
import com.njcb.ams.util.AmsDateUtils;
import com.njcb.game.repository.dao.GameRegisterDAO;
import com.njcb.game.repository.dao.GameResultDAO;
import com.njcb.game.repository.entity.GameRegisterExample;
import com.njcb.game.repository.entity.GameResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户操作类
 *
 * @author LOONG
 */
@RestController
@RequestMapping
@Api(value = "billboard", tags = "billboard")
public class GameBillboardController {

    @Autowired
    private GameResultDAO gameResultDAO;
    @Autowired
    private GameRegisterDAO registerDAO;

    @ApiOperation(value = "面板数字", notes = "面板数字")
    @RequestMapping(value = "billboard/value")
    @ResponseBody
    public EntityResponse<Map<String, String>> billboardValue() {
        Map<String, String> rtMap = new HashMap<>();
        List<GameResult> results = gameResultDAO.getMapper().selectResultWithScore();
        int count = 1;
        for (GameResult result : results) {
            if (count == 1) {
                rtMap.put("one", "第一名：" + result.getRunnersName() + "  " + result.getScore() + "分");
            } else if (count == 2) {
                rtMap.put("two", "第二名：" + result.getRunnersName() + "  " + result.getScore() + "分");
            } else if (count == 3) {
                rtMap.put("three", "第三名：" + result.getRunnersName() + "  " + result.getScore() + "分");
            }
            if (++count > 3) {
                break;
            }
        }
        Long duiwuCount = registerDAO.countByExample(new GameRegisterExample());
        GameRegisterExample baodaoExample = new GameRegisterExample();
        baodaoExample.createCriteria().andTokenIsNotNull();
        Long baodaoCount = registerDAO.countByExample(baodaoExample);
        rtMap.put("duiwushu", "报名" + duiwuCount.toString() + "人,报到" + baodaoCount.toString() + "人");
        Integer minutes = AmsDateUtils.getMinutesBetween(AmsDateUtils.getCurrentTime14(), "20230110000000");
        rtMap.put("shengyushijian", (minutes / 60) + "小时" + (minutes % 60) + "分钟");
        rtMap.put("completionrate", "90");
        return EntityResponse.build(rtMap);
    }


    @ApiOperation(value = "图表", notes = "图表")
    @RequestMapping(value = "billboard/charts")
    @ResponseBody
    public EntityResponse<Map<String, Map<String, List>>> charts() {
        Map<String, Map<String, List>> rtMap = new HashMap<>();
        rtMap.put("fenbu", getDistribution());
        rtMap.put("reallist", getRealList());
        rtMap.put("scorerate", getScoreRate());
        return EntityResponse.build(rtMap);
    }

    /**
     * 题目得分率
     *
     * @return
     */
    private Map<String, List> getScoreRate() {
        List<String> fenbuTitle = new ArrayList<>();
        fenbuTitle.add("题目一");
        fenbuTitle.add("题目二");
        fenbuTitle.add("题目三");
        fenbuTitle.add("题目四");
        List<String> fenbuData = new ArrayList<>();
        fenbuData.add("100");
        fenbuData.add("80");
        fenbuData.add("60");
        fenbuData.add("70");
        Map<String, List> scoreRateMap = new HashMap<>();
        scoreRateMap.put("title", fenbuTitle);
        scoreRateMap.put("data", fenbuData);
        return scoreRateMap;
    }

    /**
     * 参赛人员分布
     *
     * @return
     */
    private Map<String, List> getDistribution() {
        List<String> fenbuTitle = new ArrayList<>();
        fenbuTitle.add("公司1");
        fenbuTitle.add("零售");
        fenbuTitle.add("运营");
        fenbuTitle.add("创新");
        fenbuTitle.add("分行");
        List<Map<String, String>> fenbuDate = new ArrayList<>();
        HashMap<String, String> map1 = Maps.newHashMap();
        map1.put("value", "335");
        map1.put("name", "公司1");
        fenbuDate.add(map1);
        HashMap<String, String> map2 = Maps.newHashMap();
        map2.put("value", "310");
        map2.put("name", "零售");
        fenbuDate.add(map2);
        HashMap<String, String> map3 = Maps.newHashMap();
        map3.put("value", "234");
        map3.put("name", "运营");
        fenbuDate.add(map3);
        HashMap<String, String> map4 = Maps.newHashMap();
        map4.put("value", "335");
        map4.put("name", "创新");
        fenbuDate.add(map4);
        HashMap<String, String> map5 = Maps.newHashMap();
        map5.put("value", "335");
        map5.put("name", "分行");
        fenbuDate.add(map5);
        Map<String, List> fenbuMap = new HashMap<>();
        fenbuMap.put("title", fenbuTitle);
        fenbuMap.put("data", fenbuDate);
        return fenbuMap;
    }

    /**
     * 中间实时榜单
     *
     * @return
     */
    private Map<String, List> getRealList() {
        List<GameResult> results = gameResultDAO.getMapper().selectResultWithScore();
        List<String> nameList = new ArrayList<>();
        List<String> valueList = new ArrayList<>();
        int count = 1;
        for (GameResult result : results) {
            nameList.add(result.getRunnersName());
            valueList.add(result.getScore().toString());
            if (++count > 20) {
                break;
            }
        }
        Map<String, List> rtMap = new HashMap<>();
        rtMap.put("name", nameList);
        rtMap.put("value", valueList);
        return rtMap;
    }
}
